package com.smartao.base.acl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smartao.base.employee.Employee;
import com.smartao.core.dao.BaseDao;
import com.smartao.core.orm.entity.BaseEntity;
import com.smartao.core.service.BaseEntityService;

@Service
public class PermissionServiceImpl extends BaseEntityService<AccessEntry, Long> implements PermissionService {

	@Autowired
	private AccessEntryDao accessEntryDao;
	
	//TODO 该方法应考虑缓存
	@Override
	public <T extends BaseEntity<T>>Acl<T> getAclByEntity(T entity){
		Acl<T> acl = new Acl<T>(entity, accessEntryDao.findByEntityId(entity.getId()));
		return acl;
	}

	@Override
	public <T extends BaseEntity<T>>boolean isGranted(Employee user, T entity, Permission permission) {
		Acl<T> acl = getAclByEntity(entity);
		return acl.isGranted(user, permission);
	}

	@Override
	protected BaseDao<AccessEntry, Long> getDao() {
		return accessEntryDao;
	}

}
